package hgx.spring.cloud.lession.usercenter.controller;/**
 * Created by liujun on 2018/3/15.
 */


import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;
import java.util.concurrent.TimeUnit;

import hgx.spring.cloud.lession.orderserver.vo.ReponseOrderListVo;
import hgx.spring.cloud.lession.orderserver.vo.ReponseOrderVo;
import hgx.spring.cloud.lession.usercenter.service.HelloService;

/**
 * @author Administrator
 * @create 2018-03-15 19:34
 */
@RestController
@RequestMapping("/user")
public class UserController {
    private final static Random RANDOM=new Random();
    @Autowired
    private HelloService helloService;


    @GetMapping(value = "/orders")
    public ReponseOrderListVo orders(){
        ReponseOrderListVo reponseOrderListVo= helloService.orders();
        System.out.println(reponseOrderListVo.toString());
        return reponseOrderListVo;
    }

//    @HystrixCommand(fallbackMethod = "fallback")
    @HystrixCommand(fallbackMethod = "fallback",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "360")
    })
    @GetMapping(value = "/orders/{orderNo}")
    public ReponseOrderVo getOrderById(@PathVariable("orderNo") Long orderNo) throws InterruptedException {
        System.out.println("用户模块收到的请求参数 orderNo: "+orderNo);
        int sltime=RANDOM.nextInt(360);
        TimeUnit.MILLISECONDS.sleep(sltime);
        ReponseOrderVo reponseOrderVo=helloService.orders(orderNo);
//        ReponseOrderVo reponseOrderVo=new OrderHystrixCommon(com.netflix.hystrix.HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("aaaa")), helloService,orderNo).execute();
        System.out.println("用户模块响应： "+reponseOrderVo.toString());

        return reponseOrderVo;

    }

    public ReponseOrderVo fallback(Long orderNo){
        System.out.println("********************  error ");
        return new ReponseOrderVo();
    }

}
